package com.xing.servlet;

import com.xing.domain.User;
import com.xing.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 * 静，谦虚谨慎，心胸宽广容纳所有好的坏的、先苦后甜
 * day38web2
 * 2021/4/28 16:24
 */
@WebServlet(value = "/loginservlet")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        //校验
        //trim() 方法用于删除字符串的头尾空白符。修剪
        if(username==null||username.trim().length()==0){
            resp.getWriter().write("用户名不能为空");
            return;
        }
        if(pwd==null||pwd.trim().length()==0){
            resp.getWriter().write("密码不能为空");
            return;
        }
        UserServiceImpl userService = new UserServiceImpl();
        try {
            User login = userService.login(username, pwd);
            resp.sendRedirect("index.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            resp.sendRedirect("message.html");
        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
